package com.leetroll.media.job;


import com.google.gson.JsonObject;
import com.leetroll.api.service.ItemService;
import com.leetroll.common.entity.Item;
import com.leetroll.common.enums.EFileType;
import com.leetroll.common.enums.EItemType;
import com.leetroll.common.utils.FileUtils;
import com.leetroll.common.utils.JsonUtils;
import com.leetroll.media.service.UpDownloadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Objects;

import static com.leetroll.common.utils.FileUtils.toZip;

/**
 * BigMediumSmall
 * //F:\_resources\3d_model\BigMediumSmall\入库\2024-3-28-db-no_zip
 */
@Service
@Slf4j
public class Job_BMS {

    private static final String TO_DB_FILE_PATH = "F:\\_resources\\3d_model\\BigMediumSmall\\入库\\2024-3-28-db-no_zip";

    private final ItemService itemService;
    private final UpDownloadService upDownloadService;

    @Autowired
    public Job_BMS(ItemService itemService, UpDownloadService upDownloadService) {
        this.itemService = itemService;
        this.upDownloadService = upDownloadService;
    }

    /**
     * 上传所有3d资源 (不包含附件文件)
     * # 1. 按文件名翻译, 入库
     * # 2. 按文件上传规范上传到阿里云
     */
    public void upload() throws InterruptedException, IOException {
        File folder = new File(TO_DB_FILE_PATH);
        if (!folder.exists() || !folder.isDirectory()) return;

        File[] docFolders = folder.listFiles();
        if (docFolders == null) return;

        for (File itemFolder : docFolders) {
            uploadFile(itemFolder, null, null);
        }
        log.info("item 插入任务完成");
    }

    public static void main(String[] args) {
        String srcPath = "F:\\_resources\\3d_model\\BigMediumSmall\\BigMediumSmall - Wild West\\Props";
        String targetPath = "F:\\_resources\\3d_model\\BigMediumSmall\\BigMediumSmall - Wild West\\Props\\";
        for (File file : new File(srcPath).listFiles()) {
            if (file.isDirectory() && (!file.getName().equals("Weapon") || !file.getName().equals("Electric_Pole"))) {
                toZip(file.getAbsolutePath(), targetPath + file.getName() + ".zip");
            }
        }
    }

    private void uploadFile(File itemFolder, Long bundleId, String metric) {
        if (metric == null) {
            log.info("----------------------准备插入 item[{}]----------------------", itemFolder.getName());
        } else {
            log.info("\t\t>> 插入子模型: {}-{}", metric, itemFolder.getName());
        }
        File[] files = itemFolder.listFiles();
        if (files == null) return;

        //插入item表
        String name = itemFolder.getName();
        Item item = new Item();
        item.setName(name);
        item.setAlias(name);
        item.setCatId(7L);
        item.setStatus("show");
        item.setSource("BigMediumSmall");
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());

        File jsonFile = new File(itemFolder.getPath() + "\\info.json");
        if (jsonFile.exists()) {
            String jsonStr = FileUtils.getStr(jsonFile);
            JsonObject json = JsonUtils.strToJson(jsonStr);
            item.setAuthor(json.get("author").getAsString());
            item.setPrice(json.get("price").getAsDouble());
            item.setMark(json.get("mark").toString());
            item.setDesp(json.get("desp").getAsString());
            item.setTag("bundle");
            item.setType(EItemType.MODEL_BUNDLE.getType());
        } else {
            item.setAuthor("~");
            item.setPrice(15.0);
            item.setMark("");
            item.setDesp("合集模型");
            item.setBundleId(bundleId);
            item.setTag(metric);
            item.setType(EItemType.MODEL.getType());
        }
        itemService.save(item);

        for (File file : files) {
            if (file.isFile()) continue;
            if (file.getName().equals("main")) {
                //插入主图文件
                File mainFile = Objects.requireNonNull(file.listFiles())[0];
                upDownloadService.upload2AliByType(item.getId(), mainFile, EFileType.MAIN);
                if (metric == null) {
                    log.info("\t>> 主图插入成功");
                } else {
                    log.info("\t\t\t>> 主图插入成功");
                }
            } else if (file.getName().equals("prev")) {
                //插入预览图文件
                for (File preFile : Objects.requireNonNull(file.listFiles())) {
                    upDownloadService.upload2AliByType(item.getId(), preFile, EFileType.PREVIEW);
                }
                if (metric == null) {
                    log.info("\t>> 预览图插入成功");
                } else {
                    log.info("\t\t\t>> 预览图插入成功");
                }
            } else if (file.getName().equals("attach")) {
                File attachFile = Objects.requireNonNull(file.listFiles())[0];
//                File attachFile = new File("C:\\Users\\quxiaoyu\\Desktop\\test\\attach.zip");
                upDownloadService.upload2AliByType(item.getId(), attachFile, EFileType.ATTACHMENT);
                if (metric == null) {
                    log.info("\t>> 附件插入成功");
                } else {
                    log.info("\t\t\t>> 附件插入成功");
                }
            } else if (file.getName().equals("#seperate")) {
                File[] seperateFiles = file.listFiles();
                log.info("\t>> 准备插入子模型");
                for (File seperateFile : Objects.requireNonNull(seperateFiles)) {
                    File[] listFiles = seperateFile.listFiles();
                    for (File listFile : Objects.requireNonNull(listFiles)) {
                        uploadFile(listFile, item.getId(), seperateFile.getName());
                    }
                }
            }
        }
    }

}
